import sys
import os
import re

def count_zifu(file):
    """统计字符数"""
    try:
        with open(file, 'r', encoding='utf-16') as f:
            return len(f.read())
    except:
        return 0

def count_danci(file):
    """统计单词数"""
    try:
        with open(file, 'r', encoding='utf-16') as f:
            return len(re.findall(r'\b[a-zA-Z]+\b', f.read()))
    except:
        return 0

def count_juzi(file):
    """统计句子数"""
    try:
        with open(file, 'r', encoding='utf-16') as f:
            text = f.read()
            count = text.count('.') + text.count('?') + text.count('!')
            return max(1, count) if text.strip() else 0
    except:
        return 0

def count_h(file):
    """统计代码行数（非空行且非注释行）"""
    try:
        with open(file, 'r', encoding='utf-16') as f:
            lines = f.readlines()
            code_lines = 0
            for line in lines:
                stripped = line.strip()
                if stripped and not stripped.startswith('#'):
                    code_lines += 1
            return code_lines
    except:
        return 0

def count_kh(file):
    """统计空行数"""
    try:
        with open(file, 'r', encoding='utf-16') as f:
            return sum(1 for line in f if not line.strip())
    except:
        return 0

def count_zsh(file):
    """统计注释行数"""
    try:
        with open(file, 'r', encoding='utf-16') as f:
            return sum(1 for line in f if line.strip().startswith('#'))
    except:
        return 0

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("用法: python wc.py [选项] 文件名")
        print("选项: -c(字符数) -w(单词数) -s(句子数) -l(行) -k(空行) -z(注释行)")
        sys.exit(1)

    option, file = sys.argv[1], sys.argv[2]

    if not os.path.exists(file):
        print("文件不存在")
        sys.exit(1)

    if option == '-c':
        print(f"字符数: {count_zifu(file)}")
    elif option == '-w':
        print(f"单词数: {count_danci(file)}")
    elif option == '-s':
        print(f"句子数: {count_juzi(file)}")
    elif option == '-l':
        print(f"代码行数: {count_h(file)}")
    elif option == '-k':
        print(f"空行数: {count_kh(file)}")
    elif option == '-z':
        print(f"注释行数: {count_zsh(file)}")
    else:
        print("参数错误: 使用 -c, -w, -s, -l, -k 或 -z")
        sys.exit(1)